// 学生管理页面
<template>
  <div class="all">


    <el-table :data="pagination.records" border>
      <el-table-column fixed="left" prop="id" label="id" width="180"></el-table-column>
      <el-table-column prop="username" label="用户名" width="200"></el-table-column>
      <el-table-column prop="password" label="密码" width="200"></el-table-column>
      <el-table-column prop="status" label="身份" width="200"></el-table-column>
      <el-table-column prop="cost" label="租费" width="100"></el-table-column>
      <el-table-column prop="phone" label="电话" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="120"></el-table-column>
      <el-table-column prop="group_id" label="群组" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="checkGrade(scope.row.username)" type="primary" size="small" icon="el-icon-edit" circle></el-button>
          <el-button @click="deleteById(scope.row.username)" type="danger" size="small"icon="el-icon-delete" circle></el-button>
		  <el-button @click="timesById(scope.row.status)"type="warning" size="small" icon="el-icon-star-off" circle ></el-button>
        </template>
		
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[6, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page">
    </el-pagination>

    <!-- 编辑对话框-->
    <el-dialog
      title="编辑试卷信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="身份">
            <el-input v-model="form.status"></el-input>
          </el-form-item>
          <el-form-item label="租费">
            <el-input v-model="form.cost"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
		   <el-form-item label="群组">
            <el-input v-model="form.group_id"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
	
    <!-- 设置次数对话框-->
    <el-dialog
      title="设置发布问卷数量"
      :visible.sync="dialogVisible2"
      width="30%"
      :before-close="handleClose">
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="发布数量">
            <el-input v-model="this.times"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit2()">确 定</el-button>
      </span>
    </el-dialog>

	<el-row :gutter="20">
		<el-col :span="8">
			<el-input placeholder="请输入内容" v-model="sch">
				<el-button slot="append" icon="el-icon-search" @click="search(sch)"></el-button>
			</el-input>
		</el-col>
	</el-row>
	<a value=${sch}></a>

  </div>
</template>

<script>
export default {
  data() {

    return {
      pagination: {
        //分页后的考试信息
        current: 1, //当前页
        total: null, //记录条数
        size: 6, //每页条数
      },
      dialogVisible: false, //编辑信息框
	  dialogVisible2:false,//设置次数框
	  times:0,
      form: {},
	  status:[1,2],
	   //保存点击以后当前试卷的信息
	  sch:' '
    };


  },
  created() {
    this.getStudentInfo();
  },
  methods: {
	search(sch) {//按照指定用户名模糊搜索
		console.log(sch);
  if(this.$cookies.get("cstatus") == 0){
  	// this.jbb = true
	this.$axios(`/api/user/${this.pagination.current}/${this.pagination.size}`).then(res => {
			  console.log(res.data.data);
	  this.pagination = res.data.data;
	}).catch(error => {});
  } else if (this.$cookies.get("cstatus") == 1) {
	  
	  console.log(this.status[0]);
  // this.jbb2 = true
  this.$axios(`/api/user/${this.pagination.current}/${this.pagination.size}/${this.status[0]}`).then(res => {
  		  console.log(res.data.data);
    this.pagination = res.data.data;
  }).catch(error => {});
  } else if (this.$cookies.get("cstatus") == 2){
  // this.jbb3 = true
  this.$axios(`/api/user/${this.pagination.current}/${this.pagination.size}/${this.status[1]}`).then(res => {
  		  console.log(res.data.data);
    this.pagination = res.data.data;
  }).catch(error => {});
  }		
	},



    getStudentInfo() {
			if(this.$cookies.get("cstatus") == 0){
	this.$axios(`/api/user/${this.pagination.current}/${this.pagination.size}`).then(res => {
			  console.log(res.data.data);
	  this.pagination = res.data.data;
	}).catch(error => {});
			} else if (this.$cookies.get("cstatus") == 1) {
			this.$axios(`/api/user/${this.pagination.current}/${this.pagination.size}/${this.status[0]}`).then(res => {
					  console.log(res.data.data);
			  this.pagination = res.data.data;
			}).catch(error => {});
			} else if (this.$cookies.get("cstatus") == 2){
		this.$axios(`/api/user/${this.pagination.current}/${this.pagination.size}/${this.status[1]}`).then(res => {
				  console.log(res.data.data);
		  this.pagination = res.data.data;
		}).catch(error => {});
			}	
      //分页查询所有用户信息
   
    },

    //改变当前记录条数
    handleSizeChange(val) {
      this.pagination.size = val;
      this.getStudentInfo();
    },
    //改变当前页码，重新发送请求
    handleCurrentChange(val) {
      this.pagination.current = val;
      this.getStudentInfo();
    },
		
    checkGrade(username) { //修改学生信息
      this.dialogVisible = true
      this.$axios(`/api/student/${username}`).then(res => {
        this.form = res.data.data;
      })
    },
	
	
    timesById(status) { //设置用户的发布问卷次数
	
	if(this.$cookies.get("cstatus") == 1 && status == 2){
	
      this.$confirm("确定修改当前用户的发布问卷次数吗？删除后无法恢复","提醒",{
        confirmButtonText: '确定修改',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(()=> { //确认删除
        this.dialogVisible2 = true;
      }).catch(() => {
      })
	} else this.$confirm("非租户或选择的目标非用户","警告",{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      })
	  
    },
	
    deleteById(username) { //删除当前用户
      this.$confirm("确定删除当前用户吗？删除后无法恢复","Warning",{
        confirmButtonText: '确定删除',
        cancelButtonText: '算了,留着吧',
        type: 'danger'
      }).then(()=> { //确认删除
        this.$axios({
          url: `/api/student/${username}`,
          method: 'delete',
        }).then(res => {
          this.getStudentInfo()
        })
      }).catch(() => {
      })
    },
	
	
    submit() { //提交编辑信息更改
      this.dialogVisible = false
      this.$axios({
        url: '/api/student',
        method: 'put',
        data: {
          ...this.form
        }
      }).then(res => {
        console.log(res)
        if(res.data.code ==200) {
          this.$message({
            message: '更新成功',
            type: 'success'
          })
        }
        this.getStudentInfo()
      })
    }, 
	 submit2() { //提交次数更改
      this.dialogVisible = false
      this.$axios({
        url: '/api/student',
        method: 'put',
        data: {
          ...this.form
        }
      }).then(res => {
        console.log(res)
        if(res.data.code ==200) {
          this.$message({
            message: '更新成功',
            type: 'success'
          })
        }
        this.getStudentInfo()
      })
    },
	
	
    handleClose(done) { //关闭提醒
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        }).catch(_ => {});
    },
  }
};


</script>
<style lang="less" scoped>
.all {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
  .el-table tr {
    background-color: #dd5862 !important;
  }
}
.el-table .warning-row {
  background: #000 !important;
}

.el-table .success-row {
  background: #dd5862;
}
</style>
